﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Collections.ObjectModel;
using TheDrinksApp.Models;
using TheDrinksApp.Database;
using TheDrinksApp.Screen.Order;
using System.Windows.Media.Imaging;
using System.Windows.Media;

namespace TheDrinksApp.Screen
{
    public partial class OrderScreen : UserControl
    {
        private MainPage mainPage;
        private string drinkName;
        private string categoryName;
        private IceLemonYNItem iceLemon;
        private ObservableCollection<ObjectSize> listSize = new ObservableCollection<ObjectSize>();

        public ObservableCollection<ObjectSize> ListSize
        {
            get { return listSize; }
            set { listSize = value; }
        }

        public string CategoryName
        {
            get { return categoryName; }
            set 
            { 
                categoryName = value;
                if (listSize.Count > 0)
                {
                    listSize.Clear();
                }

                if (string.Equals(categoryName.ToUpper(), GlobalVariable.BEERS_AND_ALES))
                {
                    listSize.Add(Util.GetSizeByName("PINT"));
                    listSize.Add(Util.GetSizeByName("HALF PINT"));
                    listSize.Add(Util.GetSizeByName("LARGE BOTTLE"));
                    listSize.Add(Util.GetSizeByName("SMALL BOTTLE"));

                    LoadListSize();
                }
                else if (string.Equals(categoryName.ToUpper(), GlobalVariable.SOFT_DRINKS))
                {
                    listSize.Add(Util.GetSizeByName("SMALL"));
                    listSize.Add(Util.GetSizeByName("PINT"));
                    listSize.Add(Util.GetSizeByName("BOTTLE"));

                    LoadListSize();
                    //add thêm 1 cái rows có ice và lemon yes/no 
                    iceLemon = new IceLemonYNItem();
                    spListType.Children.Add(iceLemon);
                }
                else if (string.Equals(categoryName.ToUpper(), GlobalVariable.WINES))
                {
                    listSize.Add(Util.GetSizeByName("SMALL"));
                    listSize.Add(Util.GetSizeByName("LARGE"));
                    listSize.Add(Util.GetSizeByName("BOTTLE"));

                    LoadListSize();
                }
                else if (string.Equals(categoryName.ToUpper(), GlobalVariable.COCKTAILS))
                {
                    listSize.Add(Util.GetSizeByName("GLASS"));
                    listSize.Add(Util.GetSizeByName("PITCHER"));
                    LoadListSize();
                    if (Util.currentOrder.drink.Description != null)
                    {
                        TextBlock text = new TextBlock();
                        text.Foreground = new SolidColorBrush(Colors.Black);
                        text.TextWrapping = TextWrapping.Wrap;
                        text.FontSize = 25;
                        text.Text = Util.currentOrder.drink.Description;
                        text.Width = spListType.Width;
                        spListType.Children.Add(text);
                    }
                }
                else if (string.Equals(categoryName.ToUpper(), GlobalVariable.SPIRITS))
                {
                    listSize.Add(Util.GetSizeByName("SINGLE"));
                    listSize.Add(Util.GetSizeByName("DOUBLE"));
                    listSize.Add(Util.GetSizeByName("TRIPLE"));

                    LoadListSize();
                    iceLemon = new IceLemonYNItem();
                    spListType.Children.Add(iceLemon);
                }
            }
        }

        public string DrinkName
        {
            get { return drinkName; }
            set 
            { 
                drinkName = value;
                tbDrinkName.Text = drinkName.ToUpper();                
            }
        }

        private void LoadListSize()
        {
            if (spListType.Children.Count > 0)
            {
                spListType.Children.Clear();
            }
            Util.orderItems.Clear();
            for (int i = 0; i < listSize.Count; i++ )
            {
                OrderSizeItem item = new OrderSizeItem(listSize[i]);
                spListType.Children.Add(item);
            }
        }

        public OrderScreen(MainPage main)
        {
            InitializeComponent();
            mainPage = main;
            tbDrinkName.TextWrapping = TextWrapping.Wrap;

        }

        private void btnAddDrink_Click(object sender, RoutedEventArgs e)
        {
           
            int total = 0;
            for (int i = 0; i < Util.orderItems.Count; i++)
            {
                total += Util.orderItems[i].quantity;
            }
            if (total == 0)
            {
                return;
            }
           
            if (iceLemon != null)
            {
                for (int i = 0; i < Util.orderItems.Count; i++)
                {
                    Util.orderItems[i].ice = iceLemon.IsCheckIce;
                    Util.orderItems[i].lemon = iceLemon.IsCheckLemon;
                }
            }
            if (GlobalVariable.version == 1)
            {
                Util.AddToCurrentOrders();
                GlobalVariable.mainPage.SetCurrentNumber();
                mainPage.ShowPage(PAGE.CURRENT_ORDER);
            }
            else
            {
                int n = 0;
                int numOrder = 0;
                foreach (CurrentOrderDAO order in Util.orderItems)
                {
                    if (order.quantity > 0)
                    {
                        numOrder++;
                        foreach (CurrentOrderDAO curentOrder in Util.currentOrders)
                        {
                            if (order.drink.DrinkID == curentOrder.drink.DrinkID &&
                                order.mixer.MixerID == curentOrder.mixer.MixerID &&
                                order.ice == curentOrder.ice &&
                                order.lemon == curentOrder.lemon &&
                                order.size.SizeID == curentOrder.size.SizeID)
                            {
                                n++;
                                break;
                            }
                        }
                    }
                }
                
                if ((Util.currentOrders.Count + numOrder - n) > 2)
                {
                    MessageBox.Show("You only can order 2 items in free version");
                    return;
                }
                else
                {
                    Util.AddToCurrentOrders();
                    GlobalVariable.mainPage.SetCurrentNumber();
                    mainPage.ShowPage(PAGE.CURRENT_ORDER);
                }
            }
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            CategoryName = Util.currentOrder.category.Catergory;
            DrinkName = Util.currentOrder.drink.DrinkName;
        }
    }
}
